<template>
  <el-row type="flex" justify="center" align="middle" class="page-login">
    <div class="login-card">
      <h3 class="login-card-title">登录</h3>
      <el-form
        :model="form"
        :rules="rules"
        ref="loginForm"
        label-position="top"
        size="medium"
        @keyup.enter.native="handleSubmit"
      >
        <el-form-item label="用户名" prop="account">
          <el-input v-model.trim="form.account" placeholder="请输入用户名" :maxlength="50"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model.trim="form.password"
            auto-complete="off"
            placeholder="请输入密码"
            :maxlength="50"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
          <el-row type="flex">
            <el-input v-model.trim="form.captcha" auto-complete="off" placeholder="请输入验证码"></el-input>
            <GraphicCode class="captcha-img" ref="graphicCode"></GraphicCode>
          </el-row>
        </el-form-item>
        <el-form-item><el-checkbox v-model="form.rememberMe">记住我</el-checkbox></el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit" class="btn-block" :loading="$wait.is('user.login')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </el-row>
</template>

<script>
import GraphicCode from '@/components/GraphicCode';
import { mapState, mapActions } from 'vuex';

export default {
  name: 'page-login',
  components: {
    GraphicCode,
  },
  props: {
    redirectUrl: {
      type: String,
      default: '',
    },
  },
  computed: {
    ...mapState('user', ['menu']),
  },
  data() {
    const checkCaptcha = (rule, value, callback) => {
      if (!this.$refs.graphicCode.check(value)) {
        callback('验证码输入错误！');
      } else {
        callback();
      }
    };
    return {
      form: {
        account: '',
        password: '',
        captcha: '',
        rememberMe: false,
      },
      rules: {
        account: [
          {
            required: true,
            message: '请输入用户名',
          },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
          },
        ],
        captcha: [
          {
            required: true,
            message: '请输入验证码',
          },
          {
            validator: checkCaptcha,
          },
        ],
      },
    };
  },
  methods: {
    ...mapActions('user', ['login']),
    async handleSubmit() {
      await this.$refs.loginForm.validate();
      await this.login(this.form);
      this.redirect();
    },

    redirect() {
      const { redirectUrl } = this;

      if (redirectUrl) {
        this.$router.replace(redirectUrl);
      } else {
        this.$router.replace(this.menu[0].url);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.page-login {
  min-height: 100vh;
  padding: 100px;
  position: relative;
  background: url(~@/assets/login/bj.jpg) center no-repeat;
  background-size: cover;
}

.login-card {
  width: 600px;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0px 10px 40px 0px rgba(0, 0, 0, 0.2);
  border-radius: 10px;

  padding: 36px 100px;

  &-title {
    font-size: 48px;
    font-weight: bold;
    color: #333;
  }
}

.captcha-img {
  margin-left: 10px;
}
</style>
